<?php

namespace app\Api\controller;


use think\Request;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Upload extends \think\Controller
{
    public function _initialize()
    {


    }


    public function ftp(){

        $file = request()->file('file');
        $path="./uploadfiles/";

        if(!is_dir($path)){
            mkdir($path,0777,true);
        }

        $info = $file->move($path);
        if($info){

            $json['code']=200;
            $json['url']=$info->getSaveName();


        }else{

            $json['code']=400;
            $json['msg']=$info->getError();


        }


        return json($json);




    }


    public function qiniu(){

        $file = request()->file('file');
        $tmp=$file->getInfo();
        //获取扩展名
        $con=config('qiniu');
        $k=explode(".",$tmp['name']);
        $accessKey =$con['ak'];
        $secretKey = $con['sk'];
        $bucket = $con['bk'];

        $auth = new Auth($accessKey, $secretKey);

        $token = $auth->uploadToken($bucket);

        $filePath = $tmp['tmp_name'];


        $key = date('Ymd')."/".date('His').md5(time().mt_rand(1,9999)).".".$k[count($k)-1];

        $uploadMgr = new UploadManager();

         $ret= $uploadMgr->putFile($token, $key, $filePath);


          $json['url']=$con['cdn'].$ret[0]['key'];
          $json['code']=200;
          return json($json);
    }


}

